package com.yrcm.eduapp.activity.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yrcm.eduapp.R;
import com.yrcm.eduapp.activity.MSKCListActivity;
import com.yrcm.eduapp.entity.XitiBaoEntity;
import com.yrcm.eduapp.util.ImageLoader;

public class MSKCBaoAdapter extends BaseAdapter {
	private List<XitiBaoEntity> data;
	private Context mContext;
	private LayoutInflater inflater;
	private ImageLoader imageLoader;

	public MSKCBaoAdapter(Context context, List<XitiBaoEntity> list) {
		this.mContext = context;
		this.data = list;
		this.inflater = LayoutInflater.from(mContext);
		imageLoader = new ImageLoader(mContext.getApplicationContext(),
				R.drawable.moren);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (data.size() % 3 == 0) {
			return data.size() / 3;
		} else {
			return data.size() / 3 + 1;
		}
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return data.get(arg0);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View view, ViewGroup parent) {
		// TODO Auto-generated method stub
		final HolderView holder;
		if (view == null) {
			view = inflater.inflate(R.layout.mskc_kc_item, null);
			holder = new HolderView();
			holder.img1 = (ImageView) view.findViewById(R.id.img1);
			holder.img2 = (ImageView) view.findViewById(R.id.img2);
			holder.img3 = (ImageView) view.findViewById(R.id.img3);
			holder.nameTxt1 = (TextView) view.findViewById(R.id.txt1);
			holder.nameTxt2 = (TextView) view.findViewById(R.id.txt2);
			holder.nameTxt3 = (TextView) view.findViewById(R.id.txt3);
			holder.imgL1 = (LinearLayout) view.findViewById(R.id.img_l_1);
			holder.imgL2 = (LinearLayout) view.findViewById(R.id.img_l_2);
			holder.imgL3 = (LinearLayout) view.findViewById(R.id.img_l_3);
			holder.totleTxt1 = (TextView) view.findViewById(R.id.txt_1);
			holder.totleTxt2 = (TextView) view.findViewById(R.id.txt_2);
			holder.totleTxt3 = (TextView) view.findViewById(R.id.txt_3);
		} else {
			holder = (HolderView) view.getTag();
		}

		if (position == getCount() - 1) {
			switch (data.size() % 3) {
			case 0:
				holder.nameTxt1.setText(data.get(position * 3 + 0).getName());
				holder.nameTxt2.setText(data.get(position * 3 + 1).getName());
				holder.nameTxt3.setText(data.get(position * 3 + 2).getName());
				imageLoader.DisplayImage(
						data.get(position * 3 + 0).getImgUrl(), holder.img1);
				imageLoader.DisplayImage(
						data.get(position * 3 + 1).getImgUrl(), holder.img2);
				imageLoader.DisplayImage(
						data.get(position * 3 + 2).getImgUrl(), holder.img3);
				holder.totleTxt1.setText(data.get(position * 3 + 0).getTotle());
				holder.totleTxt2.setText(data.get(position * 3 + 1).getTotle());
				holder.totleTxt3.setText(data.get(position * 3 + 2).getTotle());
				break;
			case 1:
				holder.nameTxt1.setText(data.get(position * 3 + 0).getName());
				holder.totleTxt1.setText(data.get(position * 3 + 0).getTotle());

				imageLoader.DisplayImage(
						data.get(position * 3 + 0).getImgUrl(), holder.img1);
				holder.nameTxt2.setVisibility(View.INVISIBLE);
				holder.imgL2.setVisibility(View.INVISIBLE);
				holder.totleTxt2.setVisibility(View.INVISIBLE);
				holder.totleTxt3.setVisibility(View.INVISIBLE);
				holder.nameTxt3.setVisibility(View.INVISIBLE);
				holder.imgL3.setVisibility(View.INVISIBLE);
				break;
			case 2:
				holder.nameTxt1.setText(data.get(position * 3 + 0).getName());
				holder.nameTxt2.setText(data.get(position * 3 + 1).getName());
				holder.totleTxt1.setText(data.get(position * 3 + 0).getTotle());
				holder.totleTxt2.setText(data.get(position * 3 + 1).getTotle());
				imageLoader.DisplayImage(
						data.get(position * 3 + 0).getImgUrl(), holder.img1);
				imageLoader.DisplayImage(
						data.get(position * 3 + 1).getImgUrl(), holder.img2);
				holder.nameTxt3.setVisibility(View.INVISIBLE);
				holder.imgL3.setVisibility(View.INVISIBLE);
				holder.totleTxt3.setVisibility(View.INVISIBLE);
				System.out.println(position + "=======");
				break;
			default:
				break;
			}
		} else {
			holder.nameTxt1.setText(data.get(position * 3 + 0).getName());
			holder.nameTxt2.setText(data.get(position * 3 + 1).getName());
			holder.nameTxt3.setText(data.get(position * 3 + 2).getName());
			holder.totleTxt1.setText(data.get(position * 3 + 0).getTotle());
			holder.totleTxt2.setText(data.get(position * 3 + 1).getTotle());
			holder.totleTxt3.setText(data.get(position * 3 + 2).getTotle());
			imageLoader.DisplayImage(data.get(position * 3 + 0).getImgUrl(),
					holder.img1);
			imageLoader.DisplayImage(data.get(position * 3 + 1).getImgUrl(),
					holder.img2);
			imageLoader.DisplayImage(data.get(position * 3 + 2).getImgUrl(),
					holder.img3);
			holder.nameTxt2.setVisibility(View.VISIBLE);
			holder.imgL1.setVisibility(View.VISIBLE);
			holder.nameTxt1.setVisibility(View.VISIBLE);
			holder.imgL2.setVisibility(View.VISIBLE);
			holder.nameTxt3.setVisibility(View.VISIBLE);
			holder.imgL3.setVisibility(View.VISIBLE);
			holder.totleTxt1.setVisibility(View.VISIBLE);
			holder.totleTxt2.setVisibility(View.VISIBLE);
			holder.totleTxt3.setVisibility(View.VISIBLE);
			
		}

		holder.img1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mContext, MSKCListActivity.class);
				intent.putExtra("data_id", data.get(position * 3 + 0).getId());
				intent.putExtra("data_name", data.get(position * 3 + 0)
						.getName());
				mContext.startActivity(intent);
			}
		});
		holder.img2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mContext, MSKCListActivity.class);
				intent.putExtra("data_id", data.get(position * 3 + 1).getId());
				intent.putExtra("data_name", data.get(position * 3 + 1)
						.getName());
				mContext.startActivity(intent);
			}
		});
		holder.img3.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mContext, MSKCListActivity.class);
				intent.putExtra("data_id", data.get(position * 3 + 2).getId());
				intent.putExtra("data_name", data.get(position * 3 + 2)
						.getName());
				mContext.startActivity(intent);
			}
		});

		view.setTag(holder);
		return view;
	}

	class HolderView {
		ImageView img1, img2, img3;
		TextView nameTxt1;
		TextView nameTxt2;
		TextView nameTxt3;

		TextView totleTxt1, totleTxt2, totleTxt3;

		LinearLayout imgL1, imgL2, imgL3;

	}

	@Override
	public boolean isEnabled(int position) {
		return false;
	}

}
